﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TERP.Constants;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;
using CommonUtils;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmUpdatePrice : FrmMdiBase
    {

		PriceService _priceService;

        void Save()
        {
			_priceService = new PriceService();

			_priceService.Its_Price_Update(
										SystemGlobal.strUnitCodePriceBinh
										, "Bình"
										, this.GetTextBoxDoubleValue(txtPriceBinhNew).ToString()
										, DateTime.Now.ToString(Nonsense.DATE_DB_FORMAT)
										);
			SystemGlobal.strPriceBinh = this.GetTextBoxDoubleValue(txtPriceBinhNew);

			////Price for Cược vỏ
			_priceService.Its_Price_Update(
										SystemGlobal.strUnitCodePriceCuocVo
										, "Cược vỏ"
										, this.GetTextBoxDoubleValue(txtPriceCuocVoNew).ToString()
										, DateTime.Now.ToString(Nonsense.DATE_DB_FORMAT)
										);
			SystemGlobal.strPriceCuocVo = this.GetTextBoxDoubleValue(txtPriceCuocVoNew);

            //Price for Thùng
			_priceService.Its_Price_Update(
										SystemGlobal.strUnitCodePriceThung
										, "Thùng"
										, this.GetTextBoxDoubleValue(txtPriceThungNew).ToString()
										, DateTime.Now.ToString(Nonsense.DATE_DB_FORMAT)
										);

			SystemGlobal.strPriceThung = this.GetTextBoxDoubleValue(txtPriceThungNew);
        }

        bool CheckInPut()
        {
            if (StringUtils.IsEmpty(txtPriceBinhNew.Text))
            {
                Util.ShowWarningMsgBox("Nhập giá bán theo Bình");
                txtPriceBinh.Focus();
                return false;
            }

            if (StringUtils.IsEmpty(txtPriceCuocVoNew.Text))
            {
                Util.ShowWarningMsgBox("Nhập giá Cược vỏ mới");
                txtPriceBinh.Focus();
                return false;
            }

            if (StringUtils.IsEmpty(txtPriceThungNew.Text))
            {
                Util.ShowWarningMsgBox("Nhập giá bán mới theo Thùng");
                txtPriceBinh.Focus();
                return false;
            }
            return true;
        }

        public FrmUpdatePrice()
        {
            InitializeComponent();
        }               

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInPut())
                {
                    Save();
                    Util.ShowMessageBox("Đã cập nhật giá");
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }

        private void txtCuocVo_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void FrmUpdatePrice_Load(object sender, EventArgs e)
        {
            txtPriceBinh.EditValue = SystemGlobal.strPriceBinh;
            txtPriceThung.EditValue = SystemGlobal.strPriceThung;
            txtPriceCuocVo.EditValue = SystemGlobal.strPriceCuocVo;
        }
    }
}
